﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebUI.UserControls
{
    public partial class UCUploadImg : System.Web.UI.UserControl
    {
        public delegate void UploadImageEven(object sender, EventUploadImg e);

        public event UploadImageEven UploadImgHandler;

        public string ValidationGroup
        {
            get { return Button1.ValidationGroup; }
            set { Button1.ValidationGroup = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.PhysicalApplicationPath+"\\Images\\";
            string filename = null;
            try
            {
                FileInfo file = null;
                filename = txtUpload.PostedFile.FileName.Trim();
                if ((filename.Length > 0) && (txtUpload.PostedFile.ContentLength > 0))
                {
                    file = new FileInfo(filename);
                    string fileExtension = Path.GetExtension(file.Name);
                    if(fileExtension==".jpg"||fileExtension==".gif"||fileExtension==".png")
                    {
                        txtUpload.PostedFile.SaveAs(path + file.Name);
                        var MyeventArgs = new EventUploadImg(filename, path);
                        //fire event
                        UploadImgHandler(this, MyeventArgs);
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
            
        }
    }
}